import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const ControlModal 			= $Lazy.state(()=>import('./cpt/control-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '统计时间',		names: ['start_at','end_at'],	type:'date-range' },
			{ label: '科室',		name: 'project_parent_id',		type: 'select', loading:true, idStr:'uuid' },
			{ label: '岗位',		name: 'project_id',				type: 'select', loading:true },
			{ label: '迟发原因',		name: 'delay_reason' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 科室
			$fn.setRow(forms, 'project_parent_id', await f.getCache({ api:'ke_shi' } ) )
			// 岗位
			$fn.setRow(forms, 'project_id', await f.getCache({ api:'gang_wei' } ) )
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'statistics/labReportDelayCount', {
		isIndex:true,
		callback: result => this.setState({ result })
	})
	
	// 表单
	cols = [
		{ title: '科室', 		dataIndex: 'project_parent_name', 		width:100 },
		{ title: '岗位', 		dataIndex: 'project_name', 				width:100 },
		{ title: '自然项目', 	dataIndex: 'parent_kind_name', 			width:150 },
		{ title: '迟发项目', 	dataIndex: 'single_kind_name', 			width:150 },
		{ title: '迟发原因', 	dataIndex: 'delay_reason', 				width:150 },
		{ title: '数量', 		dataIndex: 'report_delay_count', 		...$fn.table.num },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '项目明细', ()=> this.modal.open({...this.model, ...rows}) )
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'statistics/labReportDelayCount'),			// 导出
			Controls.click.call(this, '项目明细', ()=> {
				let parent_kind_id = []
				let project_id = []
				let delay_reason = []
				this.state.data.forEach(v => {
					parent_kind_id.push(v.parent_kind_id)
					project_id.push(v.project_id)
					delay_reason.push(v.delay_reason)
				})
				this.modal.open({parent_kind_id: parent_kind_id ? String(parent_kind_id) : '',project_id:project_id?String(project_id):'',delay_reason:String(delay_reason)})
			} )
		]
	}
	
	render(){
		const { result } = this.state
		return (
			<>
				<Page title='实验室迟发统计' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,
						{ isInit:window.isInit,
						  isIndex:true,
						  suffix: (
							<>
								<div className='ml10 g9'>当前页迟发总数 <b className='f14 c0'>{result?.current_page_total}</b></div>
							</>
						  )
						},
					) }
				</Page>
				{/* 项目明细弹窗 */}
				<ControlModal onRef={ref=>this.modal=ref} />
			</>
		)
	}
}

export default Index